﻿using System;
using System.Collections.Generic;
using System.Linq;
using converter.template;

namespace converter
{
    public sealed class Program
    {
        public static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

            try
            {
                CommandLine commandLine = new CommandLine(args);
                RdlFile rdlFile = new RdlFile();

                foreach (string source in commandLine.Reports.Keys)
                {
                    string destination = commandLine.Reports[source];

                    Console.WriteLine(string.Format("Converting {0} => {1}", source, destination));
                    try
                    {
                        Template template = rdlFile.Parse(source, commandLine.DataSources);

                        TagsMaker tags = new TagsMaker();
                        template = tags.Process(template);

                        WordDocumentBuilder.Build(template, destination);
                    }
                    catch (Exception e)
                    {
                        // Tell about an error and continue on to the next source.
                        Console.WriteLine(e);
                    }
                }
            }
            catch (ArgumentException)
            {
                Console.Write(
@"Usage: RDLConverter.exe [options]

Options can start with a dash (-) or a slash (/). Unknown options are ignored.

-rdl:file   A report definition file. At least one file must be provided. Multiple options are allowed.
-out:file   An output (DOCX) file name. This option is ignored if multiple -rdl options are provided.
-rds:file   A datasource definition file. Multiple options are allowed.
-y          Overwrite an existing output file.
");
            }

#if DEBUG
            Wait();
#endif
        }

        // Prevent closing the command prompt window.
        private static void Wait()
        {
            Console.WriteLine("\nPress any key...");
            Console.ReadKey();
        }
    }
}
